<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use PDO;
use Think\Exception;
use app\admin\model\News as NewsModel;
use app\admin\model\Ncategory as NcategoryModel;

class News extends Api
{
    protected $noNeedRight = '*';
    protected $noNeedLogin = ['getLists','getInfo','getCate'];
    
    public function getCate()
    {
        $category = NcategoryModel::field("name,id as cate_id")->select();
        $return['category_list'] = $category;
        $this->success('查询成功', $return);
    }

    public function getLists()
    {
        $cate_id = $this->request->post('cate_id', 0);
        $where = [];
        if($cate_id > 0)
        {
            $where['category_id'] = $cate_id;
        }else{
            $category = NcategoryModel::field("name,id as cate_id")->select();
            $where['category_id'] = $category[0]['cate_id'];
        }
        $goods_name = $this->request->post('goods_name','');
        if($goods_name)
        {
            $where['goods_name'] = ['like',"%$goods_name%"];
        }
        $page = $this->request->post("page",1);
        $start = 0;
        if($page > 1)
        {
           $start = ($page - 1)*15;
        }
         //获取商品列表
        $goods_list = NewsModel::where($where)->field('id as news_id,name,visit,images,creattime')->order('creattime desc')->limit($start,65)->select();
        $return['news_list'] = $goods_list;
        $this->success('查询成功', $return);
    }
    
    
    public function getInfo()
    {
        $news_id = $this->request->post('news_id', 0);
        if($news_id == 0)
        {
            $this->error('请选择需要查看的资讯',[]);
        }
        $where['id'] = $news_id;
        $goods_info = NewsModel::where($where)->field('id as news_id,name,visit,creattime,content')->find();
        if(!$goods_info)
        {
            $this->error('资讯不存在，请重试',[]);
        }
        $return['news_info'] = $goods_info;
        $this->success('查询成功', $return);
    }
    
    
}